package de.jmda.gen.java.impl;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import de.jmda.gen.Generator;
import de.jmda.gen.impl.AbstractCompoundGenerator;
import de.jmda.gen.java.MethodDetailsGenerator;
import de.jmda.gen.java.MethodSignatureGenerator;
import de.jmda.gen.java.ThrowsClauseGenerator;

public class DefaultMethodDetailsGenerator
		extends AbstractCompoundGenerator
		implements MethodDetailsGenerator
{
	private @NotNull MethodSignatureGenerator methodSignatureGenerator;
	private ThrowsClauseGenerator throwsClauseGenerator;

	public DefaultMethodDetailsGenerator()
  {
		this(null, null);
  }

	public DefaultMethodDetailsGenerator(
      MethodSignatureGenerator methodSignatureGenerator,
      ThrowsClauseGenerator throwsClauseGenerator)
  {
	  super();
	  
	  setMethodSignatureGenerator(methodSignatureGenerator);
	  setThrowsClauseGenerator(throwsClauseGenerator);

    setComponentSeparator(System.lineSeparator());
  }

	/**
	 * Returns collection of <code>Generator</code>s with the return values
	 * of the following methods in the following order:
	 * <ul>
	 *   <li>{@link #getMethodSignatureGenerator()} and</li>
	 *   <li>{@link #getThrowsClauseGenerator()}</li>
	 * </ul>
	 * 
	 * @see de.jmda.gen.CompoundGenerator#getGeneratorComponents()
	 */
	@Override
  @NotNull
  @Valid
  public List<Generator> getGeneratorComponents()
  {
		List<Generator> result = new ArrayList<Generator>();

		result.add(getMethodSignatureGenerator());
		result.add(getThrowsClauseGenerator());

		return result;
  }

	@Override
  @NotNull
  public MethodSignatureGenerator getMethodSignatureGenerator()
  {
    return notNull(methodSignatureGenerator);
  }

	@Override
	public ThrowsClauseGenerator getThrowsClauseGenerator()
  {
	  return throwsClauseGenerator;
  }

	@Override
  public void setMethodSignatureGenerator(
      @NotNull MethodSignatureGenerator generator)
  {
		methodSignatureGenerator = notNull(generator);
  }

	@Override
  public void setThrowsClauseGenerator(ThrowsClauseGenerator generator)
  {
		throwsClauseGenerator = generator;
  }

	/**
	 * @NotNull
	 * @see de.jmda.gen.java.MethodDetailsGenerator#demandThrowsClauseGenerator()
	 */
	@Override
	public ThrowsClauseGenerator demandThrowsClauseGenerator()
  {
		throwsClauseGenerator = notNull(throwsClauseGenerator);
	  return throwsClauseGenerator;
  }

	private MethodSignatureGenerator notNull(MethodSignatureGenerator generator)
  {
		if (generator == null)
		{
			generator = new DefaultMethodSignatureGenerator();
		}

		return generator;
  }

	private ThrowsClauseGenerator notNull(ThrowsClauseGenerator generator)
  {
		if (generator == null)
		{
			generator = new DefaultThrowsClauseGenerator();
		}

		return generator;
  }
}